home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / DN114_3D.ZIP / TP_EX1.PAS < prev    next >
Pascal/Delphi Source File  |  1996-02-03  |  4KB  |  140 lines

  1. program TP_EX1;
  2.  
  3. {
  4.  
  5.   "Introduction to 3D Programming" Tutorial series
  6.   Article #1 - For DemoNews 114
  7.  
  8.   Sample code for Turbo Pascal 7.0 or above (might work with lesser versions,
  9.   probably at least 6.0, but it's untested so I can't guarantee anything).
  10.  
  11.   Make sure at least 286 instructions are enabled in your compiler options.
  12.  
  13. }
  14.  
  15. uses crt;
  16.  
  17.  
  18. { "vector" structure - holds a 3D cartesian point, and its projection. }
  19.  
  20. type
  21.     vector = record
  22.              x : integer;
  23.              y : integer;
  24.              z : integer;
  25.              scrx : integer;
  26.              scry : integer;
  27.              end;
  28.  
  29. { Box object, used for this example. }
  30. var
  31.    Box : array[0..7] of vector;  {The values are initialized later}
  32.  
  33. {
  34. Videomode and Putpixel functions for graphics display
  35.  
  36. (Yes, the putpixel is not optimized, but that's not the point of
  37.  this example program :-)
  38. }
  39.  
  40. procedure videomode(mode : word); assembler;
  41. asm
  42.    mov ax, mode
  43.    int 10h
  44. end;
  45.  
  46. procedure putpixel(x, y : word; color : byte); assembler;
  47. asm
  48.    mov ax, 0a000h
  49.    mov es, ax
  50.    mov ax, y
  51.    shl ax, 6
  52.    mov bx, ax
  53.    shl ax, 2
  54.    add ax, bx
  55.    add ax, x
  56.    mov di, ax
  57.    mov al, color
  58.    mov [es:di], al
  59. end;
  60.  
  61. {
  62. procedure PointProject(distance : integer; point : vector,
  63.                        var dest : vector; center : vector);
  64.  
  65. Takes a viewing distance from the origin along the Z axis (in our examples
  66. we've been using 256, so that's what I used in the main program), and the
  67. 3D point you want to project.  Fills in the 2D scrx and scry values of the
  68. given vector, or a separate "after projection" vector, depending on what
  69. you pass in "dest" (I used the same vector for this example).
  70.  
  71. The center vector parameter is just added to the point you give.  Stupid
  72. by itself, yes... but if you use the same center for the set of points in
  73. an object (I used a box in this program), the whole object is moved (called
  74. "translation") by the center.  Just run the example, and you'll see what I
  75. mean. :-)
  76.  
  77. }
  78.  
  79.  
  80. procedure PointProject(distance : integer; point : vector;
  81.                        var dest : vector; center : vector);
  82. begin
  83.      dest.scrx := (256*(point.x+center.x)
  84.                   div (distance-(point.z+center.z)))+160;
  85.  
  86.      dest.scry := 100-(256*(point.y+center.y)
  87.                   div (distance-(point.z+center.z)));
  88. end;
  89.  
  90. {
  91. procedure DrawBox(cenx, ceny, cenz : integer; color : byte);
  92.  
  93. Draws the Box object with the 3D center you give.  The center points are
  94. merged into the one "center" vector that PointProject wants.  All the points
  95. use the same center, which "moves" the object.
  96. }
  97.  
  98. procedure DrawBox(cenx, ceny, cenz : integer; color : byte);
  99. var
  100.    count : integer;
  101.    BoxCenter : vector;
  102. begin
  103.      BoxCenter.x := cenx;
  104.      BoxCenter.y := ceny;
  105.      BoxCenter.z := cenz;
  106.  
  107.      for count := 0 to 7 do
  108.      begin
  109.           PointProject(256, Box[count], Box[count], BoxCenter);
  110.           putpixel(Box[count].scrx, Box[count].scry, color);
  111.      end;
  112. end;
  113.  
  114.  
  115. {
  116. Main program - Draws three boxes at different 3D centers, showing how
  117.            the depth perception works.
  118. }
  119.  
  120. begin
  121.  
  122.      Box[0].x := 20;   Box[0].y := 40;   Box[0].z := 30;
  123.      Box[1].x := 20;   Box[1].y := 40;   Box[1].z := -30;
  124.      Box[2].x := 20;   Box[2].y := -40;  Box[2].z := 30;
  125.      Box[3].x := 20;   Box[3].y := -40;  Box[3].z := -30;
  126.      Box[4].x := -20;  Box[4].y := 40;   Box[4].z := 30;
  127.      Box[5].x := -20;  Box[5].y := 40;   Box[5].z := -30;
  128.      Box[6].x := -20;  Box[6].y := -40;  Box[6].z := 30;
  129.      Box[7].x := -20;  Box[7].y := -40;  Box[7].z := -30;
  130.  
  131.  
  132.      videomode($13);
  133.      DrawBox(-60, 0, -40, 13);  { Purple box, Z=-40 so it's further away }
  134.      DrawBox(  0, 0,   0, 14);  { Yellow box, Z=0 so it's at the image plane}
  135.      DrawBox( 60, 0,  40, 15);  { White box,  Z=40 so it's closer to you }
  136.      repeat until keypressed;
  137.      videomode(3);
  138. end.
  139.  
  140.